home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8017 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: argc/argv & switches
  5. Date: 28 Feb 1996 16:28:25 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h2rv9INNbc2@anvil.ugrad.cs.ubc.ca>
  8. References: <4h2j8j$9gn@milo.freenet.vancouver.bc.ca>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h2j8j$9gn@milo.freenet.vancouver.bc.ca>,
  12. Patrick Wong <zipz@opus.freenet.vancouver.bc.ca> wrote:
  13.  >I would like to know the difference between *++argv[0] and (*++argv)[0].
  14.  >Also, I've seen a lot of programs that have command-line switches like:
  15.  >
  16.  >view -r -x list.txt
  17.  >view -rx list.txt
  18.  >view -xr list.txt
  19.  >
  20.  >How can I write the view program so that it can accept all three formats?
  21.  >It has something to do with the *argv[0] parameter, I know that much.  I
  22.  >looked at K&R and found the example program confusing (find -n -l pattern).
  23.  
  24. Some systems have a function called getopt() for parsing command line options
  25. above. You would give it this string:
  26.  
  27.     "rxS:t"
  28.  
  29. for instance, to indicate that you have four options, 'r', 'x', 'S' and 't',
  30. and the 'S' option takes an argument, like -S <arg>
  31.  
  32. The GNU getopt also has a getopt_long() for long switches like "--option foo".
  33.  
  34. These are not standard C functions by any means, but you can hunt down the
  35. freeware source that you can include in your program.
  36.  
  37.  >The view program would look like this:
  38.  >/* begin program */
  39.  >#include <stdio.h>
  40.  >main()
  41.  >{
  42.  >    int c;
  43.  >    while ((c = getchar()) != EOF)
  44.  >      putchar(c);
  45.  >} /* ok, so it's a simple program... */
  46.  >/* end program */
  47.  >
  48.  >How can this program be modified to use -r and -x?
  49.  >-x : pause after a screenful of text.  If less than screnful, ignore
  50.  >-r : put line numbers in front of each line of text.
  51.  >
  52.  >This program is just something that will help me understand how the
  53.  >*argv[0] thing works.  Thanks.
  54.  
  55. In that case, do it yourself! Or look at the code for the getopt() functions in
  56. the gnu "glibc" library (which you can get from a mirror of
  57. ftp://prep.ai.mit.edu/pub/gnu.
  58.  
  59. There is also some basic argument parsing code in the K&R2 book. The FAQ might
  60. mention something as well.
  61.  
  62. -- 
  63.  
  64.